package org.ioriframework.plugin.maven.packaging.wizards;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.ioriframework.plugin.maven.Constants;
import org.ioriframework.plugin.maven.util.PreferenceUtils;
import org.ioriframework.plugin.maven.util.StringUtils;

/**
 * 打包向导页面
 * The "New" wizard page allows setting the container for the new file as well
 * as the file name. The page will only accept file name without the extension
 * OR with the extension that matches the expected one (mpe).
 */

/**
 * @author iori
 *
 */
public class PackageWizardPage extends WizardPage {
	
	private Combo envCombo;
	
	private Text earTargetText;

	private Text earText;

	public PackageWizardPage(ISelection selection) {
		super("wizardPage");
		setTitle("Maven打包");
		setDescription("选择相应环境后，点Finish按钮开始打包.");
	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 3;
		layout.marginTop = 15;
		
		// 渲染打包环境下拉框
		Label label = new Label(container, SWT.NULL);
		label.setText("打包环境:");
		this.envCombo = new Combo(container, SWT.NULL);
		this.envCombo.add("sit");
		this.envCombo.add("uat");
		this.envCombo.add("production");
		this.envCombo.select(0);
		GridData gd = new GridData();
		// 合并两个单元格
		gd.horizontalSpan = 2;
		this.envCombo.setLayoutData(gd);
		
		// 渲染包输出路径输入框
		label = new Label(container, SWT.NULL);
		label.setText("EAR包输出路径:");
		earTargetText = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		earTargetText.setLayoutData(gd);
		earTargetText.addModifyListener(new ModifyListener() {
			
			@Override
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		// 渲染文件目录选择按钮
		Button button = new Button(container, SWT.PUSH);
		button.setText("Browse...");
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				handleBrowse();
			}
		});
		
		// 渲染文件名输入框
		label = new Label(container, SWT.NULL);
		label.setText("EAR包名称:");
		earText = new Text(container, SWT.BORDER | SWT.SINGLE);
		earText.setLayoutData(gd);
		earText.addModifyListener(new ModifyListener() {
			
			@Override
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		// 向导页面初始化
		initialize();
		dialogChanged();
		setControl(container);
	}

	private void initialize() {
		earTargetText.setText(PreferenceUtils.getString(Constants.STORE_KEY_EAR_TARGET));
		earText.setText(PreferenceUtils.getString(Constants.STORE_KEY_EAR_FILE_NAME));
	}

	private void handleBrowse() {
		DirectoryDialog dialog = new DirectoryDialog(getShell());
		dialog.setText("请选择EAR包输出路径");
		// 定位到上一次打开的目录
		dialog.setFilterPath(getEarTarget());
		String earTarget = dialog.open();
		if (!StringUtils.isNullOrEmpty(earTarget)) {
			earTargetText.setText(earTarget);
		}
	}

	private void dialogChanged() {
		// 输出目录不为空时，校验正确性
		if (!StringUtils.isNullOrEmpty(getEarTarget())) {
			Path path = Paths.get(getEarTarget());
			if (!Files.isDirectory(path)) {
				updateStatus("请选择有效存在的目录");
				return;
			}
			if (!Files.isWritable(path)) {
				updateStatus("选择的目录只读，无法写入文件");
				return;
			}
		}
		String fileName = getFileName();
		// 文件名不为空时，校验正确性
		if (!StringUtils.isNullOrEmpty(fileName)) {
			if (fileName.replace("\\", "/").indexOf("/", 1) > 0) {
				updateStatus("文件名不合法");
				return;
			}
			int dotLoc = fileName.lastIndexOf(".");
			if (dotLoc != -1) {
				String ext = fileName.substring(dotLoc + 1);
				if (!ext.equalsIgnoreCase("ear")) {
					updateStatus("文件名后缀必须是.ear");
					return;
				}
			}
		}
		updateStatus(null);
	}

	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}
	
	/**
	 * 获取EAR包输出目录
	 * @return
	 */
	public String getEarTarget() {
		return StringUtils.isNullOrEmpty(earTargetText.getText()) ? "" : earTargetText.getText().trim();
	}
	
	/**
	 * 获取EAR包名称
	 * @return
	 */
	public String getFileName() {
		return earText.getText();
	}
	
	/**
	 * 获取环境信息
	 * @return
	 */
	public String getEnv() {
		return envCombo.getText();
	}

}